<?php
/*
* Description: Friends class
*/
class Friends extends DbBase{

	var $user1_id;
	var $user2_id;
	
    // constructor
	public function __construct() {
        $this->table_name = 'friends';
    }
	
	// function to check if two users are friends - returns true or false
	public function are_friends()
	{
		if($this->user1_id == $this->user2_id)
			return true;
		
		$sql  = "SELECT count(*) as count ";
		$sql .= " FROM ".$this->table_name;
		$sql .= " WHERE (user1_id=$this->user1_id AND user2_id=$this->user2_id)";
		$sql .= " OR (user2_id=$this->user1_id AND user1_id=$this->user2_id)";
		
		$result = array_shift($this->query($sql));
		if((int)$result['count'] > 0)
			return true;
		return false;
	}
	
	public function all_friends($userid){
		$result = array();
		
		$sql  = "SELECT * ";
		$sql .= " FROM ".$this->table_name;
		$sql .= " WHERE user1_id=$userid OR user2_id=$userid";
		
		$rows = $this->query($sql);
		foreach($rows as $index => $row){
			$friend = new Friends();
			$friend->set_attributes($row);
			$result[$index] = $friend;
		}
		
		return $result;
	}
	
	public function all_non_friends($userid){
		$result = array();
		
		$sql  = "SELECT * ";
		$sql .= " FROM ".$this->table_name;
		$sql .= " WHERE (user1_id!=$userid) AND (user2_id!=$userid)";
		
		$rows = $this->query($sql);
		foreach($rows as $index => $row){
			$friend = new Friends();
			$friend->set_attributes($row);
			$result[$index] = $friend;
		}
		
		return $result;
	}
	
	public function set_friends($friend1, $friend2){
		$this->user1_id = $friend1->id;
		$this->user2_id = $friend2->id;
	}
	
	public function save_friendship($friend1, $friend2){
		set_attributes($friend1, $friend2);
		//TOOD :insert
	}
	
	public function insert(){
		$sql  = "INSERT INTO $this->table_name";
		$sql .= "(user1_id, user2_id)";
		$sql .= " VALUES($this->user1_id, $this->user2_id)";
		return $this->execute($sql);
	}
	
	public function delete(){
		$sql  = "DELETE FROM $this->table_name";
		$sql .= " WHERE (user1_id=$this->user1_id AND user2_id=$this->user2_id)";
		$sql .= " OR (user2_id=$this->user1_id AND user1_id=$this->user2_id)";
		return $this->execute($sql);
	}
	
	// CLASS METHODS
	public static function are_friends_with($id1,$id2){
		$friends = new Friends();
		
		$f1 = new Users();
		$f1->find_by_id($id1);
		
		$f2 = new Users();
		$f2->find_by_id($id2);
		
		$friends->set_friends($f1, $f2);
		return $friends->are_friends();
	}
	
	public static function get_all_friends($user_id){
		$friend = new Friends();
		return $friend->all_friends($user_id);
	}
	
	public static function get_all_non_friends($user_id){
		$friend = new Friends();
		return $friend->all_non_friends($user_id);
	}
	
	public static function delete_friendships($user_id){
		$friends = new Friends();
		$friends = $friends->all_friends($user_id);
		foreach($friends as $friend)
			$friend->delete();
	}
	
	public function update(){ throw new Exception("method unimplemented yet"); }
	public function select(){ throw new Exception("method unimplemented yet"); }
}
?>